package com.lej.core.utils.extention

import java.text.SimpleDateFormat
import java.util.*
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.toDuration

/**
 * Created by 大哥哥 on 2016/10/21 0021.
 */

/**
 * 获取该时间离现在多久

 * @param format
 * *
 * @param dateTime
 * *
 * @return
 */
fun Date.getTimeSpan(timeStamp: Long = System.currentTimeMillis(), suffix: String = "前", prefix: String = ""): String {
    var timeSpan = timeStamp - time
    timeSpan /= 1000
    if (timeSpan < 60)
        return prefix + timeSpan.toString() + "秒$suffix"
    timeSpan /= 60
    if (timeSpan < 60) {
        return prefix + timeSpan.toString() + "分钟$suffix"
    }
    timeSpan /= 60
    if (timeSpan < 24) {
        return prefix + timeSpan.toString() + "小时$suffix"
    }
    timeSpan /= 24
    if (timeSpan < 30) {
        return prefix + timeSpan.toString() + "天$suffix"
    }
    timeSpan = (timeSpan / 30.42).toLong()
    if (timeSpan < 12) {
        return prefix + timeSpan.toString() + "月$suffix"
    }
    timeSpan /= 12
    return prefix + timeSpan.toString() + "年$suffix"
}

/**
 * 获取详细的该时间离现在多久
 * @return
 */
fun getTimeSpanDetail(timeStamp: Long, suffix: String = "", prefix: String = ""): String {
    val timeSpan = timeStamp - System.currentTimeMillis()/1000
    val day = timeSpan / 86400
    val hour = timeSpan / 3600 - day * 24
    val minutes = timeSpan / 60 - day * 1440 - hour * 60
    val dayStr = if (day != 0L) "${day}天" else ""
    val hourStr = if (hour != 0L) "${hour}时" else ""
    val minutesStr = if (minutes != 0L) "${minutes}分" else ""
    return "$prefix${dayStr}$hourStr$minutesStr$suffix"
}

/**
 * 获取毫秒为单位时间戳的离现在多久

 * @param format
 * *
 * @param dateTime
 * *
 * @return
 */
fun Date.getTimeSpanUntilDay(): String {
    val timeSpan = getTimeSpan()
    if (timeSpan.contains("月") || timeSpan.contains("年"))
        return date('-')
    else return timeSpan
}

fun Date.date(char: Char = '/'): String {
    return SimpleDateFormat("yyyy${char}MM${char}dd", Locale.getDefault()).format(this)
}

fun Date.year(): String {
    return SimpleDateFormat("yyyy", Locale.getDefault()).format(this)
}

fun Date.month(): String {
    return SimpleDateFormat("MM", Locale.getDefault()).format(this)
}

fun Date.day(): String {
    return SimpleDateFormat("dd", Locale.getDefault()).format(this)
}

fun Date.hour(): String {
    return SimpleDateFormat("HH", Locale.getDefault()).format(this)
}

fun Date.minute(): String {
    return SimpleDateFormat("mm", Locale.getDefault()).format(this)
}

fun Date.second(): String {
    return SimpleDateFormat("ss", Locale.getDefault()).format(this)
}

fun Date?.formatTime(format: String): String {
    if (this == null)
        return ""
    return SimpleDateFormat(format, Locale.getDefault()).format(this)
}

fun Long?.formatTime(format: String = "yyyy-MM-dd HH:mm:ss"): String {
    if (this == null)
        return ""
    return SimpleDateFormat(format, Locale.getDefault()).format(Date(this))
}

fun Long?.formatTimeBySecond(format: String): String {
    if (this == null)
        return ""
    return (this * 1000).formatTime(format)
}
/**只能精确到时分秒*/
@OptIn(ExperimentalTime::class)
fun Long?.formatTimeDurationBySecond(format: String): String {
    if (this == null)
        return ""
    val duration = this.toDouble().toDuration(DurationUnit.SECONDS)
    return duration.toComponents { _, hours, minutes, seconds, _ ->
        fun Int.toStringPad() = toString().padStart(2, '0')
        format.replace("HH", hours.toStringPad())
            .replace("mm", minutes.toStringPad())
            .replace("ss", seconds.toStringPad())
    }
}

fun main(args: Array<String>) {
    println(61L.formatTimeDurationBySecond("HH:mm:ss"))
    println(121L.formatTimeDurationBySecond("HH:mm:ss"))
    println(25L.formatTimeDurationBySecond("HH:mm:ss"))
    println(25L.formatTimeDurationBySecond("HH:mm:ss"))
}

fun Date.formatTime(format: SimpleDateFormat): String {
    return format.format(this)
}

fun String.isTodayDate(format: String): Boolean? {
    return this.toDate(format)?.isToday()
}

fun String.beforeTodayDate(format: String): Boolean? {
    return this.toDate(format)!!.date() < Date().date()
}

fun Date.isToday() = this.date() == Date().date()